﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using VkClientIM.Logic;
using VkClientIM.Windows;

namespace VkClientIM.UI
{
	/// <summary>
	/// Interaction logic for ContactListPresenter.xaml
	/// </summary>
	public partial class ContactListPresenter : UserControl
	{
		private string _currentSearchPattern = "";
		DispatcherTimer _timer=new DispatcherTimer();

		public ContactListPresenter()
		{
			InitializeComponent();
			list.ContextMenu = null;

		}

		private string[] _searchPatterns;

		private void SearchTextBoxSearch(object sender, RoutedEventArgs e)
		{
			ICollectionView view = (ICollectionView) this.list.ItemsSource;
			
			SearchTextBox box = ( SearchTextBox ) sender;
			string pattern = box.Text.ToLower();
			if (pattern == _currentSearchPattern)
			{
				e.Handled = true;
				return;
			}
			if (pattern == "")
			{
				_searchPatterns = null;
				_currentSearchPattern = pattern;
				view.Filter = Filter;
				e.Handled = true;
				return;
			}
			string[] patterns = pattern.Split(new char[] { ' ' },
			                                  StringSplitOptions.RemoveEmptyEntries);

			_searchPatterns = new string[patterns.Length * 2];

			Array.Copy(patterns, _searchPatterns, patterns.Length);
			for (int i = 0; i < patterns.Length; i++)
			{
				_searchPatterns[patterns.Length + i] = Utils.KeyboardSwitch(patterns[i]);
			}
			view.Filter = Filter;
			_currentSearchPattern = pattern;
			e.Handled = true;
			return;
		}
		
		private bool Filter(object o)
		{
			if (!(o is Logic.Profile))
			{
				return false;
			}
			Profile p = (Profile) o;
			if (_searchPatterns==null)
			{
				return true;
			}

			for (int i = 0; i < _searchPatterns.Length; i++)
			{
				string test = _searchPatterns[i];

				if (p.FirstName.ToLower().Contains(test) ||
					 p.LastName.ToLower().Contains(test) ||
					 p.NickName.ToLower().Contains(test) ||
					p.Activity.ToLower().Contains(test))
				{
					return true;
				}
			}
			return false;
		}

		private void ListItemPreviewMouse(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			if ((e.ChangedButton==MouseButton.Right)&&(e.ButtonState==MouseButtonState.Pressed))
			{
				this.contextMenu.PlacementTarget = sender as UIElement;
				e.Handled = true;
				return;
			}
			if ((e.ChangedButton == MouseButton.Right)&&(e.ButtonState==MouseButtonState.Released))
			{
				this.contextMenu.IsOpen = true;
				e.Handled = true;
			}
		}

		private void MenuItemProfileShow(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			ListViewItem item = this.contextMenu.PlacementTarget as ListViewItem;
			if (item!=null)
			{
				Profile p = item.DataContext as Profile;
				if (p != null)
				{
					Windows.ProfileWindow profileWindow=new ProfileWindow();
					profileWindow.LoadProfile( p );
					profileWindow.Show();
				}
			}
		}

		private void MenuItemSendStandAloneMessage(object sender, MouseButtonEventArgs e)
		{
			ListViewItem item = this.contextMenu.PlacementTarget as ListViewItem;
			if (item != null)
			{
				Profile p = item.DataContext as Profile;
				if (p != null)
				{
					SendStandaloneMessage window = new SendStandaloneMessage();
					window.LoadProfile(p);
					window.Show();
				}
			}
		}

		private void MenuItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
		{
			ListViewItem item = this.contextMenu.PlacementTarget as ListViewItem;
			if (item != null)
			{
				Profile p = item.DataContext as Profile;
				
				if (p != null)
				{
					App.Current.ApiLogic.BeginConversation(p);
				}
			}
		}
	}
}
